package com.itboye.ihomebank.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CaculateYearAndMonth {
	/**
	 * @Description: 字符串的日期格式的计算两个日期间相差天数
	 * @Author:caicai
	 * @Since:2016-1-29
	 * @Version:1.1
	 * @param date1
	 * @param date2
	 * @return
	 * @throws ParseException
	 */
	public static int daysBetween(String date1, String date2)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(date1));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(date2));
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * @Description: 字符串的日期格式的计算两个日期间相差月份
	 * @Author:caicai
	 * @Since:2016-1-29
	 * @Version:1.1
	 * @param date1
	 * @param date2
	 * @return
	 * @throws ParseException
	 */
	public static int monthBetween(String date1, String date2)
			throws ParseException {
		int result = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(sdf.parse(date1));
		c2.setTime(sdf.parse(date2));
		result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
		return result == 0 ? 1 : Math.abs(result);
	}

	/**
	 * @Description: 字符串的日期格式的计算两个日期间相差年份
	 * @Author:caicai
	 * @Since:2016-1-29
	 * @Version:1.1
	 * @param date1
	 * @param date2
	 * @return
	 * @throws ParseException
	 */
	public static int yearBetween(String date1, String date2)
			throws ParseException {
		int result = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(sdf.parse(date1));
		c2.setTime(sdf.parse(date2));
		result = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
		return result == 0 ? 0 : result;
	}

	/**
	 * @Description: 计算两个日期间相差的天数用XX年XX月XX日来表示
	 * @Author:caicai
	 * @Since:2016-1-29
	 * @Version:1.1
	 * @param fdate
	 *            日期1(2015-11-28)
	 * @param sdate
	 *            日期2(2016-01-29)
	 * @return
	 * @throws ParseException
	 */
	public static String calculateDays(String fdate, String sdate)
			throws ParseException {
		int year1 = 0, year2 = 0, month1 = 0, month2 = 0, day1 = 0, day2 = 0, tempY = 0, tempM = 0, tempD = 0;
		String[] str1 = fdate.split("-");
		year1 = Integer.parseInt(str1[0]);// 年份
		month1 = Integer.parseInt(str1[1]);// 月份
		day1 = Integer.parseInt(str1[2]);// 日期

		String[] str2 = sdate.split("-");
		year2 = Integer.parseInt(str2[0]);// 年份
		month2 = Integer.parseInt(str2[1]);// 月份
		day2 = Integer.parseInt(str2[2]);// 日期

		tempY = year2 - year1;// 计算年数

		if (month1 - month2 > 0) {// 计算月份
			tempY--;
			tempM = 12 - month1 + month2;
		} else if (month1 - month2 < 0) {
			tempM = month2 - month1;
		} else {
			if (day1 - day2 > 0) {
				tempY--;
				tempM=12;
			} else if (day1 - day2 < 0) {
				
			} else {
				tempD = 1;
			}
		}

		if (day1 - day2 > 0) { // 计算天数
			tempM--;
			int dat = calculateMonthDays(year1, month1);
			tempD = dat - day1 + day2;
		} else if (day1 - day2 < 0) {
			tempD = day2 - day1;
		} else {
			tempD = 1;
		}

		StringBuffer sb = new StringBuffer();
		sb.append("" + tempY);
		sb.append("-");
		sb.append(tempM);
		sb.append("-");
		sb.append(tempD);
		sb.append("");
		return sb.toString();
	}

	/**
	 * @Description:判断是否是闰年
	 * @Author:caicai
	 * @Since:2016-1-29
	 * @Version:1.1
	 * @param year
	 *            年份
	 * @return
	 */
	private static boolean isRunYear(int year) {
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
			// 是闰年
			return true;
		} else {
			// 不是平年
			return false;
		}
	}

	/**
	 * @Description: 获取当前月份的是天数
	 * @Author:caicai
	 * @Since:2016-1-29
	 * @Version:1.1
	 * @param year
	 *            年份
	 * @param month
	 *            月份
	 * @return
	 */
	private static int calculateMonthDays(int year, int month) {
		boolean isYunYear = isRunYear(year);
		int days = 31;
		switch (month) {
		case 2:
			if (isYunYear) {
				days = 29;
			} else {
				days = 28;
			}
			break;
		case 4:
			days = 30;
			break;
		case 6:
			days = 30;
			break;
		case 9:
			days = 30;
			break;
		case 11:
			days = 30;
			break;
		default:
			break;
		}
		return days;
	}

	public static long parseDate(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date_start = null;
		try {
			date_start = sdf.parse(time);
		} catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date_start.getTime();
	}
}
